package com.express.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.express.entity.Role;
import com.express.entity.RolePermission;
import com.express.mybatis.PageResult;
import com.express.mybatis.Result;
import com.express.sys.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;


@RestController
@RequestMapping({"/role"})
public class RoleController {

	@Autowired
	RoleService iRoleService;

	@PostMapping({"/add"})
	public Result add(@RequestBody Role role) {
		Result result = new Result();
		if (this.iRoleService.checkCode(role)) {
			result.error("角色编码已经存在", "300");
			return result;
		}
		this.iRoleService.add(role);
		return result;
	}

	@PostMapping({"/update"})
	public Result update(@RequestBody Role role) {
		Result result = new Result();
		if (this.iRoleService.checkCode(role)) {
			result.error("角色编码已经存在", "300");
			return result;
		}
		this.iRoleService.update(role);
		return result;
	}

	@PostMapping({"/delete"})
	public Result delete(@RequestBody JSONObject json) {
		Result result = new Result();
		String[] idsArr = json.getString("ids").split(",");
		int[] idsInt = new int[idsArr.length];
		for (int i = 0; i < idsArr.length; i++) {
			idsInt[i] = Integer.parseInt(idsArr[i]);
		}
		this.iRoleService.delete(idsInt);
		return result;
	}

	@PostMapping({"/list"})
	public PageResult getByPage(@RequestBody PageResult page) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		List<Role> list = this.iRoleService.getList();
		PageResult result = new PageResult(list);
		return result;
	}

	@PostMapping({"/listAll"})
	public Result getAll() {
		List<Role> list = this.iRoleService.getList();
		Result result = new Result();
		result.setData(list);
		return result;
	}

	@PostMapping({"/getRolePermission"})
	public Result getRolePermission(@RequestBody JSONObject json) {
		String[] roleIds = json.getString("roleId").split(",");
		Set<Map<String, Object>> roleSet = new HashSet();
		for (int i = 0; i < roleIds.length; i++) {
			List<Map<String, Object>> list = this.iRoleService.getRolePermission(Integer.valueOf(Integer.parseInt(roleIds[i])));
			roleSet.addAll(list);
		}
		Result result = new Result();
		result.setData(roleSet);
		return result;
	}

	@PostMapping({"/getRPermission"})
	public Result getRPermission(@RequestBody JSONObject json) {
		List<RolePermission> list = this.iRoleService.getRPermission(json.getInteger("roleId"));
		Result result = new Result();
		result.setData(list);
		return result;
	}

	@Transactional
	@PostMapping({"/setRPermission"})
	public Result setRPermission(@RequestBody Map<String, Object> map) {
		this.iRoleService.delRPermission(Integer.valueOf(Integer.parseInt(map.get("id").toString())));
		String permissionIds = map.get("permissionIds").toString();
		Result result = new Result();
		if ("".equals(permissionIds)) {
			return result;
		}
		String[] ids = permissionIds.split(";");
		for (int i = 0; i < ids.length; i++) {
			RolePermission rolePermission = new RolePermission();
			rolePermission.setRoleId(Integer.valueOf(Integer.parseInt(map.get("id").toString())));
			rolePermission.setPermissionId(Integer.valueOf(Integer.parseInt(ids[i])));
			this.iRoleService.setRPermission(rolePermission);
		}
		return result;
	}
}
